<template>
	<view class="login-page">
		<view class="page-input">
			<view class="">
				<view class="label-tit">手机号</view>
				<view class="input-box">
					<input type="number" v-model="phone" maxlength="11" class="f1 input_item ml-0" placeholder="请输入手机号码">
				</view>
			</view>
			<view class="">
				<view class="label-tit">验证码</view>
				<view class="input-box flex align-c">
					<input type="number" v-model="code" class="f1 input_item ml-0" placeholder="请输入验证码">
					<button class="code-btn" @click="sendCodeBtn">{{send?'获取验证码':second+'s'}}</button>
				</view>
			</view>
			<view class="">
				<view class="label-tit">登录密码</view>
				<view class="input-box">
					<input type="password" v-model="password" class="f1 input_item ml-0" placeholder="请输入登录密码">
				</view>
			</view>
			<view class="">
				<view class="label-tit">确认密码</view>
				<view class="input-box">
					<input type="password" v-model="rePassword" class="f1 input_item ml-0" placeholder="请再次输入登录密码">
				</view>
			</view>
		</view>
		<view class="page-btn">
			<button class="btn" @click="register">注册</button>
		</view>
		
		<label class="flex align-c page-agreement" @click="changeAgress">
			<checkbox :checked="isAgree" color="#FF7D1B" style="transform: scale(0.6);" />我已阅读并同意<text class="color" @click.stop="getAgreement">《用户注册协议》</text>
		</label>
		
		<u-popup :show="show" mode="center"  @close="close" @open="open">
			<scroll-view scroll-y="true" class="agreement">
				<rich-text :nodes="nodes"></rich-text>
			</scroll-view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				code: '',
				password: '',
				rePassword: '',
				s: 60,	//默认倒计时
				second: 0,
				send: true,//按钮可以点击
				isAgree: false,
				show: false,
				nodes: '',
			}
		},
		methods: {
			async register() {
				if(!this.phone) {
					this.toast('请先输入手机号')
					return false;
				}
				if(!this.code) {
					this.toast('请输入验证码')
					return false;
				}
				if(!this.password) {
					this.toast('请输入登录密码')
					return false;
				}
				if(!this.rePassword) {
					this.toast('请输入登录密码')
					return false;
				}
				if(!this.isAgree) {
					this.toast('请先勾选协议')
					return false;
				}
				let res = await this.$api.post('auth/reg', {
					mobile: this.phone,
					code: this.code,
					password: this.password,
					rpassword: this.rePassword,
					role: 2
				});
				if(res.code == 200) {
					setTimeout(v => {
						uni.navigateBack();
					},2500);
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			sendCodeBtn() { // 发送验证码
				if(!this.send){ // 防止多次重复点击
					return false;
				}
				let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
				if (!this.phone) { //判断如果手机号为空，提示用户输入手机号
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return
				} else if (!reg.test(this.phone)) { //判断手机号格式时候正确
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none'
					})
					return
				}
				this.$api.post('sms/send', {
					mobile: this.phone,
					event: 'mobilelogin'
				}).then(res => {
					if(res.code == 1) {
						this.send = false;
						uni.showToast({
							title:'发送成功',
							success: () => {
								this.time();	// 倒计时
							}
						})
					}
				})
				
			},
			// 倒计时
			time(){
				let that = this;
				that.second = that.s;
				let interval = setInterval(function(){
					if(that.second == 1){
						that.send = true;
						that.second = that.s;
						clearInterval(interval);
					}else {
						that.second--;
					}
				},1000)
			},
			open() {
				this.show = true
			},
			close() {
				this.show = false
			},
			changeAgress() {
				this.isAgree = !this.isAgree;
			},
			async getAgreement() {
				let res = await this.$api.get('user/agree', {
					type: 1
				});
				if(res.code == 200) {
					this.nodes = res.data.detail;
					this.open();
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #FFF;
	}
</style>

<style lang="scss" scoped>
	.agreement {
		width: 600rpx;
		padding: 25rpx;
		height: 60vh;
	}
	.color {
		color: #FF7D1B;
	}
	.gray {
		color: #B3B3B3;
	}
	.login-page {
		padding: 0 59rpx;
		.page-input {
			padding: 40rpx 0;
			.label-tit {
				font-size: 29rpx;
				font-weight: bold;
			}
			.input-box {
				margin: 30rpx 0;
				margin-top: 16rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #F0F0F0;
				.input_item {
					margin-left: 30rpx;
					font-size: 27rpx;
				}
				.ml-0 {
					margin-left: 0;
				}
				.code-btn {
					font-size: 27rpx;
					margin: 0;
					padding: 0;
					width: 173rpx;
					height: 60rpx;
					line-height: 60rpx;
					color: #FFF;
					background-color: #FF7D1B;
					text-align: center;
				}
			}
		}
		.page-btn {
			font-size: 27rpx;
			text-align: center;
			margin: 54rpx 0;
			.btn {
				background-color: #FF7D1B;
				color: #FFF;
				margin: 0;
				padding: 0;
				height: 87rpx;
				line-height: 87rpx;
				margin-bottom: 30rpx;
			}
		}
		.page-agreement {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 95rpx;
			font-size: 27rpx;
			text-align: center;
			margin: 0 auto;
			justify-content: center;
		}
	}
</style>